package com.lxw.library.views;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.lxw.library.bean.ColumnValue;
import com.lxw.library.bean.SpinnerInfo;
import com.lxw.library.bean.ViewAttrInfo;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;


/**
 *
 */

public class ColumnViewHelper<T> {
    Activity mContext;
    LayoutInflater mLayoutInflater;
    private LinkedHashMap<String, LxwBaseView> viewMap;
    ColumnRequestListener requestDataListener;

    public ColumnViewHelper(Activity context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        viewMap = new LinkedHashMap<>();
    }

    public LxwBaseView getColumnView(ViewAttrInfo attrInfo) {
        String type = attrInfo.getType();
        if (null == type) {
            return null;
        }
        LxwBaseView baseView;
        switch (type) {
            case "STRING":
                baseView = getEditLayout();
                break;
            case "TEXT":
                baseView = getTextLayout();
                break;
            case "HIDDEN":
                baseView = getTextLayout();
                break;
            case "INT":
                baseView = getEditLayout();
                break;
            case "TEXTAREA":
                baseView = getTEXTAREALayout();
                break;
            case "FLOAT":
                baseView = getEditLayout();
                break;
            case "SELECT":
                baseView = getSpinnerLayout();
                break;
            case "TIME":
                baseView = getDateTimeLayout(1);
                break;
            case "PHOTO":
                baseView = getPhotoView();
                break;
            case "EDIT":
                baseView = getEditLayout();
                break;
            default:
                return null;
        }
        if (null != baseView) {
            baseView.setAttrValueByAttrInfo(attrInfo);
            viewMap.put(attrInfo.getParamer(), baseView);
        }
        return baseView;
    }

    private LxwBaseView getEditLayout() {
        LxwEditTextView editText = new LxwEditTextView(mContext, requestDataListener);
        return editText;
    }

    private LxwBaseView getTEXTAREALayout() {
        LxwEditTextView editText = new LxwEditTextView(mContext, requestDataListener);
        return editText;
    }

    private LxwBaseView getTextLayout() {
        LxwTextView editText = new LxwTextView(mContext, requestDataListener);
        return editText;
    }

    private LxwBaseView getSpinnerLayout() {
        LxwSpinner mySpinner = new LxwSpinner(mContext, requestDataListener);
        return mySpinner;
    }

    private LxwBaseView getDateTimeLayout(int model) {
        LxwBaseView myTextView = new LxwDataTimeView(mContext, model, requestDataListener);
        return myTextView;
    }

    //
//
    private LxwBaseView getPhotoView() {
        LxwPhotoView myPhotoView = new LxwPhotoView(mContext, requestDataListener);
        return myPhotoView;
    }

    public boolean checkValue() {
        boolean allIsLegal = true;
        for (String key : viewMap.keySet()) {
            boolean isLegal = viewMap.get(key).checkValueIsLegal();
            if (!isLegal) {
                allIsLegal = isLegal;
                break;
            }
        }
        if (!allIsLegal) {

        }
        return allIsLegal;
    }

    public ColumnValue getColumnValues() {
        ColumnValue columnValue = new ColumnValue();
        HashMap hashMap = new HashMap<String, String>();
        HashMap spinnerMap = new HashMap<String, SpinnerInfo>();
        HashMap photoMap = new HashMap<String, List<String>>();
        columnValue.setValues(hashMap);
        columnValue.setSpinnerInfoMap(spinnerMap);
        columnValue.setPhotoMap(photoMap);
        for (String key : viewMap.keySet()) {
            LxwBaseView baseView = viewMap.get(key);
            if (baseView instanceof LxwPhotoView) {
                List<String> value = ((LxwPhotoView) baseView).getValue();
                photoMap.put(baseView.getParamer(), value);
            } else if (baseView instanceof LxwSpinner) {
                SpinnerInfo info = ((LxwSpinner) baseView).getValue();
                spinnerMap.put(baseView.getParamer(), info);
            } else {
                hashMap.put(baseView.getParamer(), baseView.getValue());
            }
        }
        return columnValue;
    }

    public String getValue(String param) {
        LxwBaseView baseView = viewMap.get(param);
        if (null != baseView) {
            return (String) baseView.getValue();
        } else {
            return "";
        }
    }

    public void setValue(String param, String value) {
        LxwBaseView baseView = viewMap.get(param);
        if (null != baseView && !TextUtils.isEmpty(value)) {
            baseView.setValue(value);
        }
    }

    //
    public void setViewClickAble(String param, boolean clickable) {
        LxwBaseView baseView = viewMap.get(param);
        if (null != baseView) {
            baseView.setViewClickAble(clickable);
        }
    }

    public void setMustParamer(String mustParamers, boolean isMust) {
        if (!TextUtils.isEmpty(mustParamers)) {
            String[] relevanceArray = mustParamers.split(",");
            for (String relevanceParamer : relevanceArray) {
                LxwBaseView baseView = viewMap.get(relevanceParamer);
                baseView.setMust(isMust);
            }
        }
    }

    public void setRequestDataListener(ColumnRequestListener requestDataListener) {
        this.requestDataListener = requestDataListener;
    }

    public LxwBaseView getBaseView(String paramer) {
        LxwBaseView baseView = viewMap.get(paramer);
        return baseView;
    }

    public void setViewValue(String paramer, String value) {
        LxwBaseView baseView = viewMap.get(paramer);
        if (null != baseView) {
            baseView.setValue(value);
        }
    }

    //
    public void setVisibility(String paramer, int visible) {
        LxwBaseView baseView = viewMap.get(paramer);
        if (null != baseView) {
            baseView.setVisibility(visible);
            if (visible == View.GONE) {
                setValue(paramer, "");
            }
        }
    }

    public void loadPhoto(String paramer, List<String> list) {
        ((LxwPhotoView) (viewMap.get(paramer))).takeSuccess(list);
    }

}
